﻿<!-- 修改于2024-05-17，新增展示部门功能 -->
<template>
  <el-cascader v-loading="isLoading"
               v-model="companyId"
               :props="{value:valueKey,emitPath:false,checkStrictly: this.checkStrictly,label:this.labelKey}"
               clearable
               filterable
               :disabled="disabled"
               @change="updateValue"
               :show-all-levels="showAllLevels"
               :options="companyTree"></el-cascader>
</template>
<script>
function noop() {}
export default {
  name: "company-select",
  // props:{
  //   title: {},action:{},headers:{},data:{},name:{},showFileList:{type:Array,default:false}
  //   ,accept:{},'on-preview':{},'on-remove':{},fileList:{type:Array,default:()=>{return []}}
  // },
  props: {
    value: {},
    dataUrl:{
      type:String,
      default:null
    },
    /**
     * 是否隐藏根节点
     */
    hideRoot:{
      type:Boolean,
      default:false,
    },
    disabled:{
      type:Boolean,
      default:false,
    },
    checkStrictly:{
      type:Boolean,
      default:true,
    },
    //数据id属性
    valueKey:{
      type:String,
      default:'id'
    },
    labelKey:{
      type:String,
      default:'name'
    },
    showAllLevels:{
      type:Boolean,
      default:false,
    },
    //是否将部门数据展示到树结构中
    showOffice:{
      type:Boolean,
      default:false
    }

  },
  model: {
    prop: 'value',
    event: 'change'
  },
    data(){
      return {
          companyTree:[],
        isLoading:true,
      }
    },
  computed:{
    companyId:{
      get(){
        return this.value;
      },
      set(value){
        this.$emit("change",value);
      }
    }
  },
  mounted(){
    this.loadCompanyTree();
  },
  methods: {
    updateValue(val){
      this.$emit('input', val)
    },
    loadCompanyTree: function () {
      let url = '/company/tree';
      if(this.dataUrl) {
        url = this.dataUrl;
      }
      this.$get(url, {}, (response) => {
        let list = response.data;
        if(this.hideRoot&&response.data&&response.data.length==1) {
          this.checkStrictly = false;
          let companyNode = response.data[0];
          if(companyNode.children&&companyNode.children.length>0) {
            list = response.data[0].children;
          }
        }

        if(this.showOffice) {
          this.traverseTree(list);
        }
        this.companyTree = list
        this.$emit("load",list);

      },()=>{
        this.isLoading = false;
      })
    },
    // 遍历树结构的方法
    traverseTree(tree) {
      // 使用递归遍历树结构
      function traverse(node) {
        if(node.officeList&&node.officeList.length>0) {
          if(!node.children){
            node.children = [];
          }
          node.children.push(...node.officeList);
          node.children.sort((a, b) => {
            if(isNaN(a.sort)||isNaN(b.sort)) {
              return 0;
            }
            return a.sort - b.sort;
          })
        }
        // 遍历子节点
        if (node.children && node.children.length > 0) {
          node.children.forEach(child => {
            traverse(child);
          });
        }
      }

      // 从根节点开始遍历
      tree.forEach(rootNode => {
        traverse(rootNode);
      });
    }
  },

};
</script>
